﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using IPTV.DBUtility;
using IPTV.module;
using System.Data;

/// <summary>
/// Summary description for PlateDao
/// </summary>
namespace IPTV.dao
{
    public class UserDao
    {
        public UserDao()
        {
            
        }

        public void addUser(me_tgsg_user user)
        {
            string sql = " INSERT INTO me_tgsg_user (id , userid , userip , registertime ,type , deleteflag )  "
                       + " VALUES ( @id , @userid , @userip , @registertime , @type ,@deleteflag )";
            
            MySqlParameter[] parms = new MySqlParameter[] 
                { 
                    new MySqlParameter( "@id" , MySqlDbType.VarChar, 36 ),
                    new MySqlParameter( "@userid" , MySqlDbType.VarChar, 36 ),
                    new MySqlParameter( "@userip" , MySqlDbType.VarChar, 20 ),
                    new MySqlParameter( "@registertime" , MySqlDbType.DateTime ),
                    new MySqlParameter( "@type" , MySqlDbType.Int32 ),
                    new MySqlParameter( "@deleteflag" , MySqlDbType.Int32)
                };
            parms[0].Value = Guid.NewGuid().ToString();
            parms[1].Value = user.UserId;
            parms[2].Value = user.UserIp;
            parms[3].Value = user.RegisterTime;
            parms[4].Value = user.Type;
            parms[5].Value = user.DeleteFlag;

            try
            {
                int aRow = MySQLHelper.ExecuteNonQuery(MySQLHelper.CONNECTION_STRING, sql, parms);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }


        public List<me_tgsg_user> getUserByUId(string userId)
        {

            string sql = " select u.id , u.userid, u.userip, u.registertime , u.type from me_tgsg_user u where u.userid = @userId AND u.deleteflag = 0 ORDER BY registertime desc ";

            MySqlParameter[] parms = new MySqlParameter[] 
                { 
                    new MySqlParameter( "@userId" , MySqlDbType.VarChar, 36 )
                };

            parms[0].Value = userId;
            List<me_tgsg_user> userList = new List<me_tgsg_user>();
            try
            {
                MySqlDataReader reader = MySQLHelper.ExecuteReader(MySQLHelper.CONNECTION_STRING, sql, parms);

                while (reader.Read())
                {
                    me_tgsg_user user = new me_tgsg_user();
                    user.Id = reader[0].ToString();
                    user.UserId = reader[1].ToString();
                    user.UserIp = reader[2].ToString();
                    user.RegisterTimeStr = reader[3].ToString();
                    user.Type =Convert.ToInt32(reader[4]);
                    user.UserId = userId;
                    userList.Add(user);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return userList;

        }

        public void addVisitRecord(me_tgsg_visitrecord record)
        {
            string sql = " INSERT INTO me_tgsg_visitrecord (id , url , idtype , entityid , grade , subject , userid , userip , createdtime) "
                       + " VALUES ( @id , @url , @idtype , @entityid , @grade ,@subject , @userid , @userip , @createdtime )";

            MySqlParameter[] parms = new MySqlParameter[] 
                { 
                    new MySqlParameter( "@id" , MySqlDbType.VarChar, 36 ),
                    new MySqlParameter( "@url" , MySqlDbType.VarChar, 36 ),
                    new MySqlParameter( "@idtype" , MySqlDbType.VarChar, 20 ),
                    new MySqlParameter( "@entityid" , MySqlDbType.VarChar, 36 ),
                    new MySqlParameter( "@grade" , MySqlDbType.VarChar, 36 ),
                    new MySqlParameter( "@subject" , MySqlDbType.VarChar, 36),
                    new MySqlParameter( "@userid" , MySqlDbType.VarChar, 36),
                    new MySqlParameter( "@userip" , MySqlDbType.VarChar, 20),
                    new MySqlParameter( "@createdtime" , MySqlDbType.DateTime)
                };
            parms[0].Value = Guid.NewGuid().ToString();
            parms[1].Value = record.Url;
            parms[2].Value = record.IdType;
            parms[3].Value = record.EntityId;
            parms[4].Value = record.Grade;
            parms[5].Value = record.Subject;
            parms[6].Value = record.Userid;
            parms[7].Value = record.Userip;
            parms[8].Value = DateTime.Now;

            try
            {
                int aRow = MySQLHelper.ExecuteNonQuery(MySQLHelper.CONNECTION_STRING, sql, parms);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    
    }
}